package com.tiny.tests;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import com.tiny.core.game.Game;
import com.tiny.core.game.GameObject;
import com.tiny.core.input.KeyboardManager;
import com.tiny.core.input.MouseManager;
import com.tiny.core.input.MousePos;
import com.tiny.core.physics.CollisionDetector;
import com.tiny.tests.common.StaticWall;
import com.tiny.utils.GameObjectManager;

/**
 * Simple sample of a ball hitting the walls
 * 
 * @author Erick Zanardo
 * 
 */
public class WallBall extends Game {

    List<GameObject> objs;
    CollisionDetector collisionDetector;
    GameObjectManager objectManager;
    int balls;

    @Override
    public void onLoad() {
        objs = new ArrayList<GameObject>();
        objectManager = new GameObjectManager(objs);
        objectManager.add(new Ball(100, 100, 10, 10));
        balls = 1;

        objectManager.add(new StaticWall(5, 30, 10, getHeight() - 50));
        objectManager.add(new StaticWall(getWidth() - 15, 30, 10, getHeight() - 50));
        objectManager.add(new StaticWall(10, 30, getWidth() - 20, 10));
        objectManager.add(new StaticWall(5, getHeight() - 30, getWidth() - 20, 10));

        collisionDetector = new CollisionDetector(objs);
    }

    @Override
    public void onUnload() {
    }

    MousePos pos;
    @Override
    public void onUpdate(int currentFrame) {
        collisionDetector.update();
        objectManager.update(currentFrame);

        if (KeyboardManager.getInstance().isPressed(KeyEvent.VK_ENTER)) {
            objectManager.add(new Ball(100, 100, 10, 10));
            balls++;
        }

        if ((pos = MouseManager.getInstance().getClick(MouseEvent.BUTTON1)) != null) {
            objectManager.add(new Ball(pos.getX(), pos.getY(), 10, 10));
            balls++;
            pos = null;
        }

        if (KeyboardManager.getInstance().isHold(KeyEvent.VK_ESCAPE)) {
            terminate();
        }
    }

    @Override
    public void onRender(Graphics2D g) {
        g.setColor(Color.LIGHT_GRAY);
        g.drawString("Press Enter to create a ball or click on the screen", 20, 20);
        g.drawString("Total of balls: " + balls, 500, 20);
        for (GameObject obj : objs) {
            obj.render(g);
        }
    }

    @Override
    public String getTitle() {
        return "Sample Wall Ball";
    }

    public static void main(String[] args) {
        new WallBall().run();
    }

}

class Ball extends GameObject {
    int velX;
    int velY;
    public Ball(int x, int y, int width, int height) {
        super(x, y, width, height);
        velX = 2;
        velY = 2;
    }

    @Override
    public void update(int currentFrame) {
        if (collidingEntities[GameObject.COLLIDING_ABOVE] != null || collidingEntities[GameObject.COLLIDING_BELOW] != null) {
            velY = velY * -1;
        }
        if (collidingEntities[GameObject.COLLIDING_LEFT] != null || collidingEntities[GameObject.COLLIDING_RIGHT] != null) {
            velX = velX * -1;
        }
        pos.x += velX;
        pos.y += velY;
    }

    @Override
    public void render(Graphics2D g) {
        g.setColor(Color.WHITE);
        g.fillOval((int)pos.x, (int)pos.y, (int)pos.width, (int)pos.height);
    }
}

